package com.guoyansoft.model.service.impl;

import com.guoyansoft.config.PasswordEncryption;
import com.guoyansoft.config.R;
import com.guoyansoft.model.dao.YardmanAndAddressDao;
import com.guoyansoft.model.dao.YardmanAndProductTeamDao;
import com.guoyansoft.model.dao.YardmanDao;
import com.guoyansoft.model.pojo.*;
import com.guoyansoft.model.service.YardmanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ：zyx
 * @date ：Created in 2023-04-26-16:06
 * @description：
 * @modified By：
 * @version: $
 */
@Service
public class YardmanServiceImpl implements YardmanService {
    @Autowired
    private YardmanDao yardmanDao;
    @Autowired
    private YardmanAndProductTeamDao ypDao;
    @Autowired
    private YardmanAndAddressDao yaDao;

    @Override
    public R save(Yardman yardman) {
        int row = yardmanDao.insert(yardman);
        return row>0?R.suc():R.fail();
    }

    @Override
    public R drop(Integer[] id) {
        int row = yardmanDao.delete(id);
        return row>0?R.suc():R.fail();
    }

    @Override
    public R modify(Yardman yardman) {
        int row = yardmanDao.update(yardman);
        return row>0?R.suc():R.fail();
    }

    @Override
    public R queryByPage(Map<String, Object> map) {
        List<Yardman> yardmen = yardmanDao.selectByPage(map);
        List<YardmanAndAddress> yardmanAndAddresses=new ArrayList<>();

        for (Yardman yardman : yardmen) {
            List<District> addresses=yardmanDao.selectAddress(yardman.getId());
            List<ProductTeam> productTeams = yardmanDao.selectProduct(yardman.getId());
            yardmanAndAddresses.add(new YardmanAndAddress(yardman.getId(),yardman,addresses,productTeams));
        }
        long total = yardmanDao.selectTotal(map);

        return R.suc(total,yardmanAndAddresses);
    }

    @Override
    public R queryAll(Map<String, Object> map) {

        return R.suc();
    }

    @Override
    public R queryAddress(Integer id) {
        List<District> addresses = yardmanDao.selectAddress(id);

        return R.suc(addresses);
    }

    @Override
    public R queryProduct(Integer id) {
        List<ProductTeam> productTeams = yardmanDao.selectProduct(id);
        return R.suc(productTeams);
    }
    @Override
    public District queryByDistrict(Map<String,Object> params) {
        District address = yardmanDao.selectByDistrict(params);
        return address;
    }
    @Override
    public R saveYardmanAndAddress(YardmanAddress yardmanAddress) {
        int insert = yaDao.insert(yardmanAddress);
        return insert>0?R.suc():R.fail();
    }

    @Override
    public R saveYardmanAndProductTeam(YardmanAndProductTeam yardmanAndProductTeam) {
        int insert = ypDao.insert(yardmanAndProductTeam);
        return insert>0?R.suc():R.fail();
    }

    @Override
    public R modifyYardmanAndAddress(Map<String, Object> map) {
        int update = yaDao.update(map);
        return update>0?R.suc():R.fail();
    }

    @Override
    public R modifyYardmanAndProduct(Map<String, Object> map) {
        int update = ypDao.update(map);
        return update>0?R.suc():R.fail();
    }

    @Override
    public R dropYardmanAndAddress(Integer[] yardmanId) {
        int delete = yaDao.delete(yardmanId);
        return delete>0?R.suc():R.fail();
    }

    @Override
    public R dropYardmanAndProduct(Integer[] yardmanId) {
        int delete = ypDao.delete(yardmanId);
        return delete>0?R.suc():R.fail();
    }

    @Override
    public Yardman queryById(Integer id) {
        Yardman yardman = yardmanDao.selectById(id);
        return yardman;
    }


    @Override
    public R selectByLogin(Yardman yardman) {
        yardman.setPass(PasswordEncryption.getCiphertext(yardman.getPass()));
        Yardman yardman1 = yardmanDao.selectByLogin(yardman);
        return yardman1==null?R.fail():R.suc(yardman1);
    }
}
